<template>
  <div class="login-wrap">
    <el-form
      class="login-container"
      :model="jshUser"
      :rules="rules"
      ref="jshUser"
    >
      <h1 class="title">用户注册</h1>
      <el-form-item prop="loginName">
        <el-input
          type="text"
          placeholder="请输入用户名"
          v-model="jshUser.loginName"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="" prop="password">
        <el-input
          type="password"
          placeholder="请输入用户密码"
          v-model="jshUser.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="" prop="password1">
        <el-input
          type="password"
          placeholder="请输入确认密码"
          v-model="jshUser.password1"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('jshUser')"
          style="width: 48%"
          >用户注册</el-button
        >
        <el-button style="width: 48%" @click="gotoLogin">返回登陆</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import userApi from '@/api/user'

export default {
  name: "register",
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      } else {
        userApi.checkIsLoginNameExist(this.jshUser.loginName)
          .then(res => {
            if (res.status) {
              callback('用户名已存在')
            } else {
              callback()
            }
          })
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.jshUser.password !== "") {
          callback()
        }
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.jshUser.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback()
      }
    };
    return {
      jshUser: {
        loginName: "",
        password: "",
        password1: "",
      },
      rules: {
        password: [
          { validator: validatePass, trigger: "blur" },
          { min: 6, message: '密码由6位数字、小写字母组成!', trigger: 'blur' }
        ],
        password1: [{ validator: validatePass2, trigger: "blur" }],
        loginName: [{ validator: checkName, trigger: "blur" }],
      },
    };
  },
  methods: {
    gotoLogin: function () {
      this.$router.push({ path: "/login" });
    },
    submitForm(jshUser) {
        this.$refs[jshUser].validate((valid) => {
          if (valid) {
            userApi.registerUser(this.jshUser)
              .then(res => {
                this.$message({
                  message: '注册成功',
                  type: 'success'
                })
                this.$router.push({ path: "/login" })
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
  },
};
</script>

<style scoped="scoped">
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>
